#include "mainwindow.h"
#include <QApplication>
#include <QDesktopWidget>
#include "basketballwidget.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QDebug>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QFile>
#include "barackapp.h"
#include "addbasketballtodbwidget.h"
#include "addogbaskeytodbwidget.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{

    InitDataBase();
    resize(1280,818);

    QPalette palette;

    palette.setBrush(QPalette::Background,
                     QBrush(QPixmap(":/img/img/RefBackground.png").scaled(QApplication::desktop()->width()
                                                              ,QApplication::desktop()->height())));
    this->setPalette(palette);

    m_TabWidget = new QTabWidget(this);

    m_TabWidget->addTab(new BasketballWidget(this),/*tr("篮球")*/"BasketBall");
    m_TabWidget->addTab(new AddBasketBallToDBWidget(this),"BasketBallDB");
    m_TabWidget->addTab(new AddOGBaskeyToDBWidget(this),"OGbassketBallDB");
    setCentralWidget(m_TabWidget);
 //   m_TabWidget->addTab(new QWidget(),tr("足球
 //   m_TabWidget->resize(/*QApplication::desktop()->*/width(),/*QApplication::desktop()->*/height());
   /* QGridLayout *mainLayout = new QGridLayout();

    mainLayout->addWidget(m_TabWidget);
    setLayout(mainLayout);*/


}


void MainWindow::InitDataBase()
{
    QString DatabaseFilePath = BarackApp::FindDatabaseFile();

    bool db_exists = QFile(DatabaseFilePath).exists();

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(DatabaseFilePath);

    if (!db.open())
    {
        qDebug() << "Open database failed!";
        exit(0);
    }


    if(!db_exists)
    {
        QSqlQuery q;
        QString q_cmd;
        q_cmd = "CREATE TABLE t_basketboll (\"ID\" INTEGER PRIMARY KEY AUTOINCREMENT, ";
        q_cmd += "\"Date\" date, ";
        q_cmd += "\"CompareType\" VARCHAR(256), ";
        q_cmd += "\"HomeTeam\" VARCHAR(256), ";
        q_cmd += "\"VisitingTeam\" VARCHAR(256), ";
        q_cmd += "\"Result\" VARCHAR(256), ";
        q_cmd += "\"Comparefraction\" VARCHAR(256) ,";
        q_cmd += "\"Recoed\" TEXT ,";
        q_cmd += "\"Multiple\" VARCHAR(256) )";

        qDebug() << "q_cmd : " << q_cmd;
        if (q.exec(q_cmd) == false)
        {
            qDebug() << "Create database failed!";
        }

        q_cmd = "CREATE TABLE t_basketboll_history (\"ID\" INTEGER PRIMARY KEY AUTOINCREMENT,\"Date\" date,  \"VisitingTeam\" VARCHAR(256), \"HomeTeam\" VARCHAR(256),\"Result\" VARCHAR(256), \"IsNBA\" VARCHAR(256) )";
        if (q.exec(q_cmd) == false)
        {
            qDebug() << "Create database failed!";
        }
        q_cmd = "CREATE TABLE t_og_basketball_history (\"ID\" INTEGER PRIMARY KEY AUTOINCREMENT,\"Date\" date,  \"VisitingTeam\" VARCHAR(256), \"HomeTeam\" VARCHAR(256),\"Result\" VARCHAR(256), \"IsNBA\" VARCHAR(256) )";
        if (q.exec(q_cmd) == false)
        {
            qDebug() << "Create database failed!";
        }
        //q_cmd = "CREATE TABLE t_basketboll_history ("ID" INTEGER PRIMARY KEY AUTOINCREMENT, "Date" date,  "VisitingTeam" VARCHAR(256), "HomeTeam" VARCHAR(256),"Result" VARCHAR(256), "IsNBA" VARCHAR(256) )"
        //q_cmd = "CREATE TABLE t_og_basketball_history ("ID" INTEGER PRIMARY KEY AUTOINCREMENT, "Date" date,  "VisitingTeam" VARCHAR(256), "HomeTeam" VARCHAR(256),"Result" VARCHAR(256), "IsNBA" VARCHAR(256) )"
    }


}

MainWindow::~MainWindow()
{


}

bool MainWindow::event(QEvent *event)
{
   /* int type = event->type();
    if(event->type() == QEvent::Show)
    {
        m_TabWidget->resize(width(),height());

    }*/
    return QMainWindow::event(event);
}
